
package com.netease.xone.widget.tabview;

import protocol.XoneService;
import protocol.meta.LoopBack;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;

/**
 * 主页Tab
 */
public class MainTab extends AbsTabView {
    // 资讯
    public static final int INDEX_NEWS = 0;
    // 订阅
    public static final int INDEX_SUBS = 1;
    // 广场
    public static final int INDEX_PLAZA = 2;
    // 我
    public static final int INDEX_ME = 3;
    // 设置
    public static final int INDEX_OPTION = 4;
    

    public MainTab(Context context) {
        super(context);
        init();
    }

    public MainTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    private void init() {
        setBackgroundResource(R.drawable.nav_bg);
    }

    @Override
    public int getTabCount() {
        return 5;
    }

    @Override
    public int getTabDividerStyle() {
        return DIVIDER_NONE;
    }

    @Override
    public int getTabDividerResource() {
        return 0;
    }

    @Override
    public View getTabView(int index) {
        View v = View.inflate(mContext, R.layout.tab_main_view, null);
        v.setClickable(true);
        
        TextView tv = (TextView)v.findViewById(R.id.textview);
        if (index == 0) {
            tv.setText(R.string.main_tab_news);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.main_tab_news_selector, 0, 0);
        } else if (index == 1) {
            tv.setText(R.string.main_tab_subs);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.main_tab_subs_selector, 0, 0);
        } else if (index == 2) {
            tv.setText(R.string.main_tab_plaza);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.main_tab_plaza_selector, 0, 0);
        } else if (index == 3) {
            tv.setText(R.string.main_tab_me);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.main_tab_me_selector, 0, 0);
        } else if (index == 4) {
            tv.setText(R.string.main_tab_option);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.main_tab_option_selector, 0, 0);
        }
        
        // 默认是隐藏消息图标
        v.findViewById(R.id.imageview).setVisibility(View.GONE);
        
        return v;
    }

    /**
     * 显示消息
     * @param index
     */
    public static void showMsg(int index) {
        sendMsg(index, View.VISIBLE);
    }
    
    /**
     * 隐藏消息
     * @param index
     */
    public static void hideMsg(int index) {
        sendMsg(index, View.GONE);
    }
    
    private static void sendMsg(int index, int visibility) {
        Param param = new Param();
        param.index = index;
        param.visibility = visibility;
        
        LoopBack lb = new LoopBack();
        lb.mType = XoneConsts.LOOPBACK_TYPE.maintab_msg;
        lb.mData = param;
        
        XoneService.getInstance().doLoopBack(lb);
    }
    
    /**
     * 设置消息显示状态
     * @param index
     * @param visibility
     */
    public void setMsgVisibility(int index, int visibility) {
        mTabViews[index].findViewById(R.id.imageview).setVisibility(visibility);
    }
    
    public static class Param {
        public int index;
        public int visibility;
    }
}
